<template>
  <div class="goodsclass">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="height: 100%;">
      <el-button @click="addGoodsClass">添加分类</el-button>
      <tree-table
        :columns="columns"
        :data="cateList"
        :expand-type="false"
        :selection-type="false"
        border
        index-text="#"
        show-index
      >
        <template #isok="{row}">
          <i v-if="row.cat_deleted" class="el-icon-success"></i>
          <i v-else class="el-icon-error"></i>
        </template>
        <template #order="{row}">
          <el-tag v-if="row.cat_level===0" type="success">一级</el-tag>
          <el-tag v-else-if="row.cat_level===1" type="warning">二级</el-tag>
          <el-tag v-else type="danger">三级</el-tag>
        </template>
        <template #caozuo="{row}">
          <el-button @click="edit(row,'edit')">编辑</el-button>
          <el-button @click="edit(row,'del')">删除</el-button>
        </template>
      </tree-table>
      <el-pagination
        :current-page="form.pagenum"
        :page-size="form.pagesize"
        :page-sizes="[5, 6, 7, 8]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange">
      </el-pagination>
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      title="提示"
      width="70%">
      <el-form v-if="str==='edit'">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cat_name"></el-input>
        </el-form-item>
      </el-form>
      <el-form v-else>
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="cat_name">
          <el-cascader
            v-model="value"
            :options="attrs"
            :props=catePropsl
            expand-trigger="hover"
            @change="handleChange"></el-cascader>
        </el-form-item>
      </el-form>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { addGoodsClass2, delGoodsClass, editGoodsClass, getGoodsClass } from '@/api/goods'

export default {
  name: 'hm-goodsclass',
  data () {
    return {
      type: 2,
      catePropsl: {
        label: 'cat_name',
        children: 'children',
        value: 'cat_id'
      },
      value: [],
      attrs: [],
      cat_name: '',
      dialogVisible: false,
      total: 0,
      form: {
        type: 3,
        pagenum: 1,
        pagesize: 2
      },
      form3: {
        type: 2,
        pagenum: 1,
        pagesize: 10
      },
      cateList: [],
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '牌序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'caozuo'
        }
      ],
      str: '',
      cat_id: ''
    }
  },
  created () {
    this.getgoodsClass()
  },
  methods: {
    // 编辑删除区域
    async edit (row, str) {
      this.str = str
      this.cat_name = row.cat_name
      this.cat_id = row.cat_id
      if (this.str === 'edit') {
        this.dialogVisible = true
      } else if (this.str === 'del') {
        await this.$confirm('确认删除数据吗')
        const res = await delGoodsClass(row.cat_id)
        this.$message.info(res.meta.msg)
        this.getgoodsClass()
      }
    },
    async submit () {
      const res = await addGoodsClass2({
        cat_level: 2,
        cat_name: this.cat_name,
        cat_pid: this.value[1]
      })
      this.dialogVisible = false
      this.$message(res.meta.msg)
      this.getgoodsClass()
      if (this.str === 'edit') {
        const res = await editGoodsClass(this.cat_id, {
          cat_name: this.cat_name
        })
        this.$message(res.meta.msg)
        this.dialogVisible = false
        this.getgoodsClass()
      }
    },
    // 下拉菜单
    handleChange () {

    },
    // 添加分类
    async addGoodsClass () {
      this.str = ''
      this.cat_name = ' '
      this.dialogVisible = true
      const res = await getGoodsClass(this.form3)
      this.attrs = res.data.result
    },
    // 获得数据和分页
    async getgoodsClass () {
      const res = await getGoodsClass(this.form)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监听pagesize
    handleSizeChange (newsize) {
      this.form.pagesize = newsize
      this.getgoodsClass()
    },
    handleCurrentChange (newPage) {
      this.form.pagenum = newPage
      this.getgoodsClass()
    }
  }
}
</script>

<style lang="scss" scoped>
.goodsclass {

}
</style>
